package com.mebs.model.logistics;

import org.jt.jet.util.JetUtils;

import javax.persistence.*;
import java.util.Set;
import java.util.Date;

import com.mebs.model.Address;
import com.mebs.model.Warehouse;
import com.mebs.model.inventory.AbstractOrder;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Feb 22, 2006, 9:00:28 PM
 */
@Entity()
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "STORAGE")
@PrimaryKeyJoinColumn(name = "Id")
public class Storage extends LogisticsOperation {
    @ManyToOne(optional = false)
    @JoinColumn(name = "AddressId")
    private Address address;

    @ManyToOne(optional = true)
    @JoinColumn(name = "StatusId")
    private StorageStatus status;

    @Column(name = "GeneralCargoStorage", nullable = true, length = 250)
    private String generalCargoStorage;

    @Column(name = "GeneralCargoStorageLongTerm", nullable = true)
    private Boolean generalCargoStorageLongTerm;

    @Column(name = "HouseholdGoods", nullable = true, length = 250)
    private String householdGoods;

    @Column(name = "HouseholdGoodsLongTerm", nullable = true)
    private Boolean householdGoodsLongTerm;

    @Column(name = "FamilyName", nullable = true, length = 250)
    private String familyName;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "StorageId")
    @OrderBy("dateIn")
    private Set<Lot> lots;

    @ManyToOne(optional = true)
    @JoinColumn(name = "WarehouseId")
    private Warehouse warehouse;


    /**
     * Getters-setters
     */

    public Set<Lot> getLots()
    {
        return lots;
    }

    public void setLots(Set<Lot> lots)
    {
        this.lots = lots;
    }

    public Warehouse getWarehouse()
    {
        return warehouse;
    }

    public void setWarehouse(Warehouse warehouse)
    {
        this.warehouse = warehouse;
    }

    public String getGeneralCargoStorage()
    {
        return generalCargoStorage;
    }

    public void setGeneralCargoStorage(String generalCargoStorage)
    {
        this.generalCargoStorage = generalCargoStorage;
    }

    public Boolean getGeneralCargoStorageLongTerm()
    {
        return generalCargoStorageLongTerm;
    }

    public void setGeneralCargoStorageLongTerm(Boolean generalCargoStorageLongTerm)
    {
        this.generalCargoStorageLongTerm = generalCargoStorageLongTerm;
    }

    public String getHouseholdGoods()
    {
        return householdGoods;
    }

    public void setHouseholdGoods(String householdGoods)
    {
        this.householdGoods = householdGoods;
    }

    public Boolean getHouseholdGoodsLongTerm()
    {
        return householdGoodsLongTerm;
    }

    public void setHouseholdGoodsLongTerm(Boolean householdGoodsLongTerm)
    {
        this.householdGoodsLongTerm = householdGoodsLongTerm;
    }

    public String getFamilyName()
    {
        return familyName;
    }

    public void setFamilyName(String familyName)
    {
        this.familyName = familyName;
    }

    public Address getAddress()
    {
        return address;
    }

    public void setAddress(Address address)
    {
        this.address = address;
    }

    public StorageStatus getStatus()
    {
        return status;
    }

    public void setStatus(StorageStatus status)
    {
        this.status = status;
    }

    @Transient
    public boolean isOrderExcludable(AbstractOrder order) {
        boolean excludable = true;
        for(Lot lot : this.lots) {
            if(lot.isOrderUsed(order)) {
                excludable = false;
                break;
            }
        }
        return excludable;
    }

    @Transient
    public Type getRequirementType()
    {
        return Type.STORAGE;
    }

    @Transient
    public Date getEarliestDateIn()
    {
        if (JetUtils.isEmpty(lots)) return null;

        Date date = null;
        for (Lot lot : lots) {
            if ((date == null) || (date.after(lot.getDateIn()))) date = lot.getDateIn();
        }
        return date;
    }

    @Override
    public String toString()
    {
        return (this.getSequence() != null) ? this.getSequence().toString() : "";
    }

/*
    @Transient
    @Deprecated
    public Integer getBoxedQuantity(OrderItem orderItem)
    {
        if (JetUtils.isEmpty(lots)) return 0;

        int qty = 0;
        for (Lot lot : lots) {
                qty += lot.getBoxedQuantity(orderItem);
        }

        return qty;
    }

    @Transient
    @Deprecated
    public Integer getBoxedQuantity(BoxSubItem subItem)
    {
        if (JetUtils.isEmpty(lots)) return 0;

        int qty = 0;
        for (Lot lot : lots) {
                qty += lot.getBoxedQuantity(subItem);
        }

        return qty;
    }
*/

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(this.lots);
    }

    @Transient
    public String getSequenceNumber()
    {
        return String.format("STO-%s-%05d",
            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
            getSequence());
    }


}
